package com.bai.edu.controller;


import com.bai.edu.entity.Chapter;
import com.bai.edu.entity.chapter.ChapterVo;
import com.bai.edu.service.ChapterService;
import com.bai.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author bai
 * @since 2023-07-25
 */
@RestController
@RequestMapping("/eduservice/chapter")
public class ChapterController {
    @Autowired
    private ChapterService chapterService;

    //课程大纲列表
    @GetMapping("getChapterVideo/{courseId}")
    public R getChapter(@PathVariable String courseId) {
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("list", list);
    }

    //添加
    @PostMapping("addChapter")
    public R addChapter(@RequestBody Chapter chapter) {
        chapterService.save(chapter);
        return R.ok();
    }

    //根据id查询章节
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfoById(@PathVariable String chapterId) {
        Chapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", chapter);
    }

    //修改章节
    @PostMapping("updateChapter")
    public R updateChapterInfo(@RequestBody Chapter chapter) {
        chapterService.updateById(chapter);
        return R.ok();
    }

    //删除章节
    @DeleteMapping("remove/{chapterId}")
    public R removeChapter(@PathVariable String chapterId) {
        boolean b = chapterService.removeChapter(chapterId);
        if (b) {
            return R.ok();
        } else {
            return R.erro();
        }
    }
}

